package com.wac.data.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wac.data.service.StateService;
import com.wac.data.service.UserService;
import com.wac.data.utils.DataListUtil;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
@RestController
@RequestMapping("/state")
public class StateController {

	private static ObjectMapper objectMapper = new ObjectMapper();

	@Resource
	private StateService stateService;
	@Resource
	private UserService userService;

	@RequestMapping("/isFollow")
	public String isFollow(String id, HttpServletRequest request) {
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		if (stateService.isFollow(id, userId)) {
			return "1";
		}
		return "0";
	}

	@RequestMapping("/isFriend")
	public String isFriend(String id, HttpServletRequest request) {
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		if (stateService.isFriend(id, userId)) {
			return "1";
		}
		return "0";
	}

	@RequestMapping("/toFollow")
	public String toFollow(String id, HttpServletRequest request, String type) {
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		if (stateService.toFollow(id, userId, type)) {
			return "1";
		}
		return "0";
	}

	@RequestMapping("/addView")
	public String addView(String id, int type) {
		if (stateService.addView(id, type)) {
			return "1";
		}
		return "0";
	}


	@RequestMapping("/user/bangumi")
	public String getBangumiList(int page, String id) throws JsonProcessingException {
		return objectMapper.writeValueAsString(stateService.getUserBangumiList(page, id));
	}

	@RequestMapping("/user/error")
	public String errorMsg() throws JsonProcessingException {
		Map<String, Object> res = new HashMap<>();
		res.put("code", 0000);
		res.put("msg", "需要登录");
		return objectMapper.writeValueAsString(res);
	}

	@RequestMapping("/user/reLogin")
	public String reLoginMsg() throws JsonProcessingException {
		Map<String, Object> res = new HashMap<>();
		res.put("code", 0001);
		res.put("msg", "重复登录");
		return objectMapper.writeValueAsString(res);
	}
}

